<?php
namespace Common\Model;
use Think\Model;
class FavoriteModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_favorite'; 
	
	public function getFavoriteList($data){
		if(!empty($data['custid'])){
			$map['custid']  = $data['custid'];
		}
		
		if(!empty($data['moduleid'])){
			$map['moduleid']  = $data['moduleid'];
		}
		
		
		$total = $this->where($map)->count();
		$fields = array('favoriteid','fcustid','targetid','moduleid',
				'imgurl','title','content','url','author','createtime','updatetime');
		
		if(empty($data['sortField']))$data['sortField'] = "favoriteid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
		
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else 
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
		
		return array('total'=>$total , 'data'=>$result);
	}
	
	public function checkFavorite($data){
		$map['custid'] = $data['custid'];
		$map['targetid'] = $data['targetid'];
		$map['moduleid'] = $data['moduleid'];
		$result = $this->where($map)->find();
		if(!empty($result)) return 1;
		else return 0;
	}
	
	public function getFavoriteById($data){
		$map['favoriteid'] = $data['favoriteid'];
		$map['custid'] = $data['custid'];
		$result = $this->where($map)->find();
		return $result;
	}
	
	public function getFavoriteByHash($data){
		$map['favoritehash'] = $data['favoritehash'];
		$map['custid'] = $data['custid'];
		$result = $this->where($map)->find();
		return $result;
	}
	
	
	public function addFavorite($data){
		$result = $this->add($data);
		return $result;
	}
	
	
	public function deleteFavorite($data){
		$map['favoriteid'] = $data['favoriteid'];
		$map['custid'] = $data['custid'];
		$result = $this->where($map)->delete($data);
		return $result;
	}
}


